package org.jaaksi.test.jpush.ui;

import android.app.NotificationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.jaaksi.test.jpush.JPushReceiver;
import org.jaaksi.test.jpush.MyApplication;
import org.jaaksi.test.jpush.R;
import org.jaaksi.test.jpush.base.BaseActivity;
import org.jaaksi.test.jpush.model.push.MsgPushFeedBean;
import org.jaaksi.test.jpush.util.ConstantUtils;
import org.jaaksi.test.jpush.util.EventBusTool;
import org.simple.eventbus.Subscriber;

/**
 * Created by Fcy on 2016/1/24. 房源，分为二手、新房
 */
public class HouseFeedActivity extends BaseActivity
        implements View.OnClickListener
{

	private TextView tv_tip_ershou, tv_tip_new;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		// 清除通知栏的消息，不要再这里清除吧，可不可以设置点击通知的时候清除，从通知调过来的intent是什么，能不能判断
		((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
		        .cancel(JPushReceiver.REPLY_NOTIFY_ID);
		EventBusTool.register(this);
		setContentView(R.layout.activity_source_house);
		initView();
		initPoint();
	}

	private void initView()
	{
		findView(R.id.tab_ershou).setOnClickListener(this);
		findView(R.id.tab_new).setOnClickListener(this);
		tv_tip_ershou = findView(R.id.tv_tip_ershou);
		tv_tip_new = findView(R.id.tv_tip_new);

	}

	@Subscriber(tag = ConstantUtils.EventTag.UPDATE_SOURCE_HOUSE)
	public void updateWidthTag(String msg)
	{
		initPoint();
	}

	/**
	 * 初始化小红点
	 */
	private void initPoint()
	{
		MsgPushFeedBean pushFeedBean = MyApplication.getInstance().mSpFactory
		        .getHousePushFeedData();
		if (pushFeedBean != null)
		{
			if (pushFeedBean.unReadErShou == 0)
			{
				tv_tip_ershou.setVisibility(View.GONE);
			}
			else
			{
				tv_tip_ershou.setVisibility(View.VISIBLE);
				tv_tip_ershou.setText(pushFeedBean.unReadErShou + "");
			}

			if (pushFeedBean.unReadNewHouse == 0)
			{
				tv_tip_new.setVisibility(View.GONE);
			}
			else
			{
				tv_tip_new.setVisibility(View.VISIBLE);
				tv_tip_new.setText(pushFeedBean.unReadNewHouse + "");
			}
		}
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		EventBusTool.unregister(this);
	}

	@Override
	public void onClick(View v)
	{
		MsgPushFeedBean pushFeedBean = MyApplication.getInstance().mSpFactory
		        .getHousePushFeedData();

		switch (v.getId())
		{
			case R.id.tab_ershou:
				// TODO 这里模拟点击就清楚，真实中是跳转到指定界面，数据拉去成功后清除
				// 先清除二手tab, 再清除列表MessageListActivity, 再清除 MainActivity 消息tab
				if (pushFeedBean.unReadErShou == 0)
				{ // 如果没有消息，则不再更新point，不再发送消息
					return;
				}
				pushFeedBean.unReadErShou = 0;
				break;
			case R.id.tab_new:
				if (pushFeedBean.unReadNewHouse == 0)
				{
					return;
				}
				pushFeedBean.unReadNewHouse = 0;
				break;
		}
		Toast.makeText(this, "数据拉去成功后，清除小红点", Toast.LENGTH_SHORT).show();

		MyApplication.getInstance().mSpFactory
		        .setHousePushFeedData(pushFeedBean);
		initPoint();
		// 将清除小红点的消息发送出去,通知其他页面更新数据
		EventBusTool.post("", ConstantUtils.EventTag.UPDATE_SOURCE_HOUSE);
		EventBusTool.post("", ConstantUtils.EventTag.UPDATE_POINT);
	}
}